package com.damie.web.controller;


import com.damie.common.commonutils.R;
import com.damie.common.dao.User;
import com.damie.common.dto.UserDto;
import com.damie.common.paramer.UserParamer;
import com.damie.web.service.UserService;
import com.damie.web.utils.JwtUtil;
import com.damie.web.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户",tags = "{增加用户积分和回应用户反馈}")
@CrossOrigin("*")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result login(@RequestBody UserDto userDto,HttpServletResponse response){
//        {
//            "userId": "1351497901649453058",
//                "userPassword": "ls"
//        }
        System.out.println("111111");
        UserParamer userParamer = new UserParamer();
        BeanUtils.copyProperties(userDto,userParamer);
        User user = userService.login(userParamer);

        if (user!=null){
            String userId = user.getUserId();
            String token = JwtUtil.createToken(userId);
            System.out.println("令牌:"+token);
            response.addHeader("Access-Control-Expose-Headers","token");
            response.addHeader("token", token);
            return Result.success("登陆成功",token);
        }else {
            return Result.fail("账号或者密码错误");
        }
    }
    /**
     * 订单完成,增加用户积分
     */
    @ApiOperation("积分")
    @PostMapping("addIntegral")
    public R addIntegral(String wasteOrderId){
        userService.addIntegral(wasteOrderId);
        return R.ok().message("增加成功");
    }
}

